import requests

from common.config import getconf
from tests.new_rypark_background.get_cookie import internal_token


class ParkingLot:
    # =====================停车场管理=====================

    def create_parking_lot(self, env, building_type=1, surroundings=1, img_url=""):
        """创建停车场"""
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/parkingLot/create"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        dict = {
            "name": '自动化测试新建车场',  # 停车场名称
            "organizationName": "自动化测试运营单位",  # 运营单位
            "buildingType": building_type,  # 停车场类型 1地下停车场、2地面停车场、3路边停车场
            "surroundings": surroundings,  # 停车场场景 1商业中心、2办公楼、3住宅小区、4工业园区、5医院、6车站、7机场、8公共场所
            "districtCode": 310115,  # 地区编码
            "districtHierarchicalName": "中国,上海,上海市,浦东新区",  # 地区
            "detailedAddress": "上海市浦东新区申江路5005弄",  # 地址
            "geoLat": "31.209344",
            "geoLng": "121.626178",
            "responsibleName": "张三",  # 负责人
            "responsiblePhoneNo": "1600000000",  # 联系电话
            "imgUrl": img_url
        }
        return requests.post(url, headers=headers, json=dict).json()

    def query_parking_lot_list(self, env, name="", status=-1, current=1, size=12):
        """停车场列表"""
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/parkingLot/list/page"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        dict = {
            "name": name,
            "status": status,  # 1-启用状态 9-停用 -1-全部
            "current": current,
            "size": size
        }
        return requests.post(url, headers=headers, json=dict).json()

    def update_parking_lot(self, env, id, building_type=1, surroundings=1, img_url=""):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/parkingLot/update"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        dict = {
            "name": '自动化测试新建车场',
            "organizationName": "自动化测试运营单位",
            "buildingType": building_type,
            "surroundings": surroundings,
            "districtCode": 310115,
            "districtHierarchicalName": "中国,上海,上海市,浦东新区",
            "detailedAddress": "上海市浦东新区盛夏路169号1幢1层118A室",
            "geoLat": "31.212694",
            "geoLng": "121.629665",
            "id": id,
            "responsibleName": "张三",
            "responsiblePhoneNo": "16000000000",
            "imgUrl": img_url
        }
        return requests.post(url, headers=headers, json=dict).json()

    def enable_parking_lot(self, env, id, status=1):
        """启用/停用停车场"""
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-parking-server/parkingLot/update"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "token": internal_token(env)}
        data = {
            "id": id,
            "status": status  # 9-停用 1-启用
        }
        return requests.post(url, headers=headers, json=data).json()
